# -*- coding: utf-8 -*-
'''
Created on 20.06.2019

@author: yu03
'''

from FFT_Interpolation import *
from Interpolation_Test import x, tau0, N

f_set = np.linspace(5, 30, 1001)
phi = 0

f_estimated = []
f_useforphase = []
phase_estimated = []
f_diff = []
f_error = []

for f in f_set:
    sig = np.cos(2*np.pi*f*x + phi)
    a, b, c = FFT_interpolation_boxcar(sig, tau0)
    f_estimated.append(a)
    phase_estimated.append(b)
    f_useforphase.append(c)
    f_diff.append(f-a) ### Error between Estimated Freq and The Freq used for Phase cal
    f_error.append(f-c) ### Error between real value and The Freq used for Phase cal
    
    
plt.figure('Boxcar_FreqScan')
plt.gcf().set_size_inches(12,12)
plt.subplot(3,1,1)
plt.plot(f_set, f_set, label='Real Freq.', color='black', linestyle='solid', linewidth=1, marker=' ', fillstyle='full', markeredgecolor='black', markeredgewidth=0.0)
plt.plot(f_set, f_estimated, label='Estimated Freq.', color='blue', linestyle='solid', linewidth=1, marker=' ', fillstyle='full', markeredgecolor='blue', markeredgewidth=0.0)
plt.plot(f_set, f_useforphase, label='Freq. used in Phase Estimation', color='red', linestyle='solid', linewidth=1, marker=' ', fillstyle='full', markeredgecolor='red', markeredgewidth=0.0)
legend = plt.legend(loc='upper left')
plt.setp(legend.get_texts()[0], color = 'black')
plt.setp(legend.get_texts()[1], color = 'blue')
plt.setp(legend.get_texts()[2], color = 'red')
plt.title("Frequency Estimation")
plt.ylabel("Calculated Frequency")
plt.xlabel("Real Frequency")
plt.grid(which='major', axis='both')

plt.subplot(3,1,2)
plt.plot(f_set, f_diff, label='Estimated Freq. Error', color='blue', linestyle='solid', linewidth=1, marker=' ', fillstyle='full', markeredgecolor='blue', markeredgewidth=0.0)
plt.plot(f_set, f_error, label='Freq. Error for Phase', color='red', linestyle='solid', linewidth=1, marker=' ', fillstyle='full', markeredgecolor='red', markeredgewidth=0.0)
legend = plt.legend(loc='upper right')
plt.setp(legend.get_texts()[0], color = 'blue')
plt.setp(legend.get_texts()[1], color = 'red')
plt.title("Frequency Error")
plt.ylabel("Frequency Error")
plt.xlabel("Real Frequency")
plt.grid(which='major', axis='both')

plt.subplot(3,1,3)
plt.plot(f_set, phase_estimated, label='Phase Error', color='red', linestyle='solid', linewidth=1, marker=' ', fillstyle='full', markeredgecolor='red', markeredgewidth=0.0)
legend = plt.legend(loc='upper right')
plt.setp(legend.get_texts(), color = 'red')
plt.title("Phase Error")
plt.ylabel("Phase Error")
plt.xlabel("Real Frequency")
plt.grid(which='major', axis='both')

plt.tight_layout()
plt.show()
# plt.savefig('Boxcar_FreqScan.png', dpi=300)
